<template>
    <div>
        <el-table
          :data="tableData"
          @updateData="initTableData"
          border
          style="width: max-content"
          
          stripe
        >
            <el-table-column
              prop="id"
              label="试卷编号"
              width="80"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="create_time"
              label="创建时间"
              width="150">
            </el-table-column>
            <el-table-column
              prop="update_time"
              label="修改时间"
              width="150">
            </el-table-column>
            <el-table-column
              prop="name"
              label="姓名"
              width="120">
            </el-table-column>
            <el-table-column
              prop="paper_class"
              label="分类"
              width="120"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="paper_grade"
              label="期次"
              width="120"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="difficulty"
              label="难度"
              align="center"
              width="80">
            </el-table-column>
            <el-table-column
              prop="status"
              label="试卷状态"
              width="100"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="total_score"
              label="总分"
              align="center"
              width="120">
            </el-table-column>
            <el-table-column
              prop="use_count"
              label="使用次数"
              align="center"
              width="80">
            </el-table-column>
            <el-table-column
              prop="create_man"
              label="创建用户"
              width="80">
            </el-table-column>
            <el-table-column
              fixed="right"
              label="操作"
              width="120">
                <template slot-scope="scope">
                    <el-button @click="handleClick(scope)" type="text" size="small">删除</el-button>
                    <el-button type="text" size="small" @click="callChild(scope.row)">编辑</el-button>
                    <el-button type="text" size="small" @click="routePush('/detail',scope.row)">详情</el-button>
                </template>
            </el-table-column>
        
        </el-table>
        <edit-table-dialog ref="child" @updated="dialogUpdated"></edit-table-dialog>
    </div>
</template>

<script>
import editTableDialog from "@c/editTableDialog";
import { deleteData } from "@/utils/Util";

export default {
    name: "edit-table",
    components: { editTableDialog },
    methods: {
        handleClick(row) {
            console.log(row);
            // 删除处理逻辑
            deleteData("/paper/" + row.row.id).then(result => {
                console.log("数据表::", result);
                if (result === 1) {
                    this.tableData.splice(row.$index, 1);
                }
            });
        },
        callChild(obj) {
            // 向模态框子组件传值
            this.$refs.child.setVisible(true);
            console.log(obj);
            let form = this.$refs.child.form;
            this.objDataCopy(obj, form);
            // for (const objKey in form) {
            //     form[objKey] = obj[objKey];
            // }
            this.cacheObj = obj;
        },
        // 将旧对象的值拷贝到新对象中, 需要有相同的键
        objDataCopy(oldObj, newObj) {
            for (const oldObjKey in oldObj) {
                newObj[oldObjKey] = oldObj[oldObjKey];
            }
        },
        initTableData(listData) {
            // console.log("初始化数据::", listData);
            this.tableData = listData;
        },
        dialogUpdated(res) {
            this.objDataCopy(res, this.cacheObj);
        },
        routePush(url, obj) {
            // console.log("当前行对象:;",obj);
            this.$router.push(url);
            sessionStorage.setItem("rowSubjectInfo", JSON.stringify(obj));
            let parse = JSON.parse(sessionStorage.rowSubjectInfo);
            console.log("题目::", JSON.parse(parse.subject));
        }
        
    },
    
    data() {
        return {
            dialogFormVisible: false,
            
            tableData: [],
            // 缓存传向模态框的对象
            cacheObj: {}
        };
    }
};
</script>

<style scoped lang="scss">

</style>